package com.loyal.mwm.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.loyal.core.ui.activity.BasicActivity;
import com.loyal.mwm.impl.IContactImpl;

public abstract class BaseActivity<T extends ViewDataBinding> extends BasicActivity implements IContactImpl {
    protected ProgressDialog progressDialog;
    protected T binding;

    public abstract @LayoutRes
    int actLayoutRes();

    @Override
    public void setLayoutOrView() {
        binding = DataBindingUtil.setContentView(this, actLayoutRes());
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initDialog();
    }

    private void initDialog() {
        progressDialog = new ProgressDialog(this);
        Window window = progressDialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.7f;// 透明度
            lp.dimAmount = 0.8f;// 黑暗度
            window.setAttributes(lp);
        }
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
    }

    public void showProgressDialog() {
        showProgressDialog(null);
    }

    public void showProgressDialog(CharSequence message) {
        if (null != progressDialog) {
            progressDialog.setMessage(replaceText(message));
            progressDialog.show();
        }
    }

    public String getUsername() {
        return getIntent().getStringExtra("username");
    }

    public void dismissDialog() {
        if (null != progressDialog) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
